--- permalink: /python/offline layout: user-guide page_type: u-guide description: How to use Plotly offline inside IPython notebooks with Plotly Offline name: Plotly Offline for IPython Notebooks language: python has_thumbnail: false thumbnail: /images/static-image --- {% raw %}

Plotly Offline

Plotly Offline brings interactive Plotly graphs to the offline IPython Notebook environment.

Instead of saving the graphs to a server, your data and graphs will remain inside your IPython notebook. When your ready to share, you can just publish them to the Plotly Cloud or to your company's internal Plotly Enterprise.

To get started with Plotly Offline, start a trial or contact sales@plot.ly to learn about licensing and receive a URL to download the package.

In [ ]:
! pip install plotly --upgrade
In [1]:
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot

print __version__ # requires version >= 1.7.5
1.7.6
In [2]:
# download_plotlyjs('your_licensed_plotly_offline_url') # Contact sales@plot.ly to receive a valid URL
Success! Now start an IPython notebook and run the following code to make your first offline graph:

import plotly
plotly.offline.init_notebook_mode() # run at the start of every ipython notebook
plotly.offline.iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
In [2]:
init_notebook_mode() # run at the start of every ipython notebook
In [3]:
iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
Drawing...
In [4]:
from plotly.graph_objs import *
import numpy as np
In [5]:
iplot([Box(y = np.random.randn(50), showlegend=False) for i in range(45)])
Drawing...
In [7]:
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
       Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))])
Drawing...
In [8]:
# Plotting with Pandas

import pandas as pd
df = pd.read_csv('https://plot.ly/~etpinard/191.csv')
df.head(1)
Out[8]:
Africa_Life Expentancy [in years] Africa_text Africa_marker.size Africa_Gross Domestic Product per Capita [in USD of the year 2000] Americas_Life Expentancy [in years] Americas_text Americas_marker.size Americas_Gross Domestic Product per Capita [in USD of the year 2000] Asia_Life Expentancy [in years] Asia_text Asia_marker.size Asia_Gross Domestic Product per Capita [in USD of the year 2000] Europe_Life Expentancy [in years] Europe_text Europe_marker.size Europe_Gross Domestic Product per Capita [in USD of the year 2000] Oceania_Life Expentancy [in years] Oceania_text Oceania_marker.size Oceania_Gross Domestic Product per Capita [in USD of the year 2000]
0 72.301 Country: Algeria <br>Life Expectancy: 72.30... 33333216 6223.367465 75.32 Country: Argentina <br>Life Expectancy: 75.... 40301927 12779.37964 43.828 Country: Afghanistan <br>Life Expectancy: 4... 31889923 974.580338 76.423 Country: Albania <br>Life Expectancy: 76.42... 3600523 5937.029526 81.235 Country: Australia <br>Life Expectancy: 81.... 20434176 34435.36744
In [9]:
iplot({
    'data': [
        Scatter(x=df[continent+'_Life Expentancy [in years]'],
                y=df[continent+'_Gross Domestic Product per Capita [in USD of the year 2000]'],
                text=df[continent+'_text'],
                marker=Marker(size=df[continent+'_marker.size'], sizemode='area', sizeref=131868,),
                mode='markers',
                name=continent) for continent in ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
    ],
    'layout': Layout(xaxis=XAxis(title='Life Expectancy'), yaxis=YAxis(title='GDP per Capita', type='log'))
})
Drawing...
In [ ]:
# An alternative method for Pandas plotting is with cufflinks: https://github.com/santosjorge/cufflinks
! pip install cufflinks --upgrade
In [10]:
import cufflinks as cf
In [6]:
iplot(cf.datagen.lines().iplot(asFigure=True,
                               kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))
Drawing...
In [8]:
iplot(cf.datagen.heatmap(20,20).iplot(asFigure=True,
                                      kind='heatmap',colorscale='spectral',title='Cufflinks - Heatmap'))
Drawing...
In [11]:
import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

airports = [ dict(
        type = 'scattergeo',
        locationmode = 'USA-states',
        lon = df_airports['long'],
        lat = df_airports['lat'],
        hoverinfo = 'text',
        text = df_airports['airport'],
        mode = 'markers',
        marker = dict( 
            size=2, 
            color='rgb(255, 0, 0)',
            line = dict(
                width=3,
                color='rgba(68, 68, 68, 0)'
            )
        ))]
        
flight_paths = []
for i in range( len( df_flight_paths ) ):
    flight_paths.append(
        dict(
            type = 'scattergeo',
            locationmode = 'USA-states',
            lon = [ df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i] ],
            lat = [ df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i] ],
            mode = 'lines',
            line = dict(
                width = 1,
                color = 'red',
            ),
            opacity = float(df_flight_paths['cnt'][i])/float(df_flight_paths['cnt'].max()),
        )
    )
    
layout = dict(
        title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
        showlegend = False, 
        height = 900,
        geo = dict(
            scope='north america',
            projection=dict( type='azimuthal equal area' ),
            showland = True,
            landcolor = 'rgb(243, 243, 243)',
            countrycolor = 'rgb(204, 204, 204)',
        ),
    )
    
fig = dict( data=flight_paths + airports, layout=layout )

iplot(fig)
Drawing...
In [12]:
import plotly.plotly as py # all methods in plotly.plotly will communicate with a Plotly Cloud or Plotly Enterprise

# get_figure downloads a figure from plot.ly or Plotly Enterprise. 
# You need to provide credentials to download figures: https://plot.ly/python/getting-started/
fig = py.get_figure('https://plot.ly/~jackp/8715', raw=True)
iplot(fig)
Drawing...
{% endraw %}